/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: future_error_domain.h
 *
 * Purpose: Specifies the types of internal errors that can occur upon
 *          calling Future::get or Future::Get Result. These definitions
 *          are equivalent to the ones from std::future_errc
 *
 * Developer:
 *   wen.gu , 2020-04-10
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __ARA_CORE_FUTURE_ERROR_DOMAIN_H__
#define __ARA_CORE_FUTURE_ERROR_DOMAIN_H__
/******************************************************************************
 **    INCLUDES
 ******************************************************************************/


#include "ara/core/error_domain.h"
#include "ara/core/exception.h"
#include "ara/core/error_code.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/
#ifndef CORE_ERR_CASE
#define CORE_ERR_CASE(err) case err: ret = #err; break
#endif

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace ara
{
namespace core
{

 enum class future_errc: ErrorDomain::CodeType
{
    broken_promise = 101, /** the asynchronous task abandoned its shared state */
    future_already_retrieved = 102, /** the contents of the shared state were already accessed */
    promise_already_satisfied = 103, /** attempt to store a value into the shared state twice */
    no_state = 104, /** attempt to access Promise or Future without an associated state */
};

/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
/** Exception type thrown by Future and Promise classes. */
class FutureException : public Exception
{
public:
    explicit FutureException(ErrorCode err) noexcept
        :Exception(err)
    {
        /** todo something */
    }

};

/** Error domain for errors originating from classes Future and Promise.  */
class FutureErrorDomain final : public ErrorDomain
{
public:
    using Errc = future_errc;
    using Exception = FutureException;

public:
    constexpr FutureErrorDomain () noexcept
        :ErrorDomain(0x8000000000000013ull)
    {
        /** todo something */
    }

public:
    char const* Name() const noexcept override { return "Future"; }
    char const* Message(ErrorDomain::CodeType errorCode) const noexcept override
    {
        char const* ret = "";
        Errc err = static_cast<Errc>(errorCode);
        switch (err)
        {
        CORE_ERR_CASE(Errc::broken_promise);
        CORE_ERR_CASE(Errc::future_already_retrieved);
        CORE_ERR_CASE(Errc::promise_already_satisfied);
        CORE_ERR_CASE(Errc::no_state);
        default: break;
        }

        return ret;
    }
    void ThrowAsException(ErrorCode const& errorCode) const noexcept(false) override
    {
        throw Exception(errorCode);
    }

public:
     //static constexpr FutureErrorDomain mErrDomain;

    static const FutureErrorDomain& GetInstance()
    {
        constexpr static const FutureErrorDomain fem;
        return fem;
    }

    constexpr FutureErrorDomain const& GetRef() const { return *this; }
};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

constexpr inline ErrorDomain const& GetFutureErrorDomain() noexcept
{
    return FutureErrorDomain::GetInstance().GetRef();
}
constexpr inline ErrorCode MakeErrorCode(ara::core::future_errc code, ErrorDomain::SupportDataType data) noexcept
{
    return std::move(ErrorCode(static_cast<ErrorDomain::CodeType>(code), GetFutureErrorDomain(), data));
}


} /** namespace core */
} /** namespace ara */

#endif /** !__ARA_CORE_FUTURE_ERROR_DOMAIN_H__ */

